package com.sskj.alarm;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class PostRequestExample {
    public static void main(String[] args) {
        try {
            URL url = new URL("http://127.0.0.1:25110/DevCtrl/DeviceAction"); // 设置POST请求的URL
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");

            // 设置请求头部信息
            conn.setRequestProperty("Content-Type", "text/plain");

            // 设置请求体数据
            String param = "param={\n" +
                    "    \"DeviceId\": 1000,\n" +
                    "    \"ActionType\": 1\n" +
                    "}"; // 请求参数
            try (OutputStream os = conn.getOutputStream()) {
                byte[] input = param.getBytes("utf-8");
                os.write(input, 0, input.length);
            }

            // 读取响应
            try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"))) {
                StringBuilder response = new StringBuilder();
                String responseLine = null;
                while ((responseLine = br.readLine()) != null) {
                    response.append(responseLine.trim());
                }
                System.out.println(response.toString());
            }

            conn.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
